﻿using System;
using System.Collections.Generic;
using System.Text;
using JinTai.MemorialTablets.Interfaces;
using NHibernate.Burrow.AppBlock.DAOBases;
using NHibernate.Criterion;

namespace JinTai.MemorialTablets.Dao.NHibernateImple
{
    public sealed class ContactDao : GenericDAO<Contact>, IContactDao
    {
        protected override Order DefaultOrder
        {
            get { return Order.Asc("Name"); }
        }

        #region IDao<Contact,Guid> 成员

        public Contact Get(Guid id)
        {
            return base.Get(id);
        }

        public Contact Load(Guid id)
        {
            return base.Load(id);
        }

        public IList<Contact> FindAll(int startRow, int pageSize)
        {
            return base.FindAll(startRow, pageSize, "Name");
        }

        public IList<Contact> Search(string mobile, string homePhone,
            string contactName, int pageIndex, int pageSize, out int totalNumber)
        {
            var criteria = CreateCriteria();
            if (!String.IsNullOrEmpty(mobile))
                criteria.Add(Restrictions.Like("Mobile", mobile + "%"));
            if (!String.IsNullOrEmpty(homePhone))
                criteria.Add(Restrictions.Like("HomePhone", "%" + mobile + "%"));
            if (!String.IsNullOrEmpty(contactName))
                criteria.Add(Restrictions.Like("Name", "%" + contactName + "%"));

            criteria.SetFirstResult(pageSize * pageIndex);
            criteria.SetMaxResults(pageSize);

            var result = criteria.List<Contact>();
            //get totalNumber
            criteria.SetProjection(Projections.RowCount());
            criteria.SetFirstResult(0);
            criteria.SetMaxResults(int.MaxValue);
            totalNumber = criteria.UniqueResult<Int32>();

            return result;

        }

        public IList<Contact> GetContacts(Guid[] ids)
        {
            var criteria = CreateCriteria();
            criteria.Add(Restrictions.In("Id", ids));
            return criteria.List<Contact>();
        }

        #endregion
    }
}